home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 089a.dms / 089a.adf / example.lha / Termites.AMOS / Termites.amosSourceCode
AMOS Source Code  |  1992-02-26  |  6KB  |  208 lines

  1. ' ********************   
  2. ' *     Termites     * 
  3. ' * By Mike Richmond * 
  4. ' ******************** 
  5. '
  6. ' Version 1.00 
  7. '
  8. ' Termites is a semi-random movement generator. "How interesting!" I am sure 
  9. ' you are saying to yourself, but if you run the program a few times, with 
  10. ' different values for NUMBER (the number of termites in each base) and
  11. ' speed (the maximum speed the termites can dig at), it is actually quite
  12. ' fascinating. 
  13. '
  14. ' You have all seen fractals - random but regular patterns created by complex
  15. ' mathematics.  Well although termites is not quite as complex it is 
  16. ' incredible how complex the patterns can become.
  17. '
  18. ' Programs like this one are used to research artificial intelligence at its 
  19. ' most basic level.  The termites in this program can dig up, down, left or
  20. ' right through the soil, at a speed of 0 to <SPEED> pixels.  They start 
  21. ' out from an invisible termite hill and spread out randomly, digging tunnels
  22. ' as they go (OK, OK, it should have been called earthworm...)  Wherever a 
  23. ' red termite goes, the soil goes red; wherever a blue one goes the soil turns 
  24. ' blue.  If they invade each others tunnels the part they travel on turns to 
  25. ' their colour.
  26.  
  27. '
  28. ' If you leave it for a long time (like an month:) you would eventually find 
  29. ' that evrey pixel was covered by either a red or blue trail.  And apparently, 
  30. ' in the AI research labs, the termites actually seem to work together,  
  31. ' creating ordered tunnels.
  32. '
  33. ' In this cut down version (there are no deaths, births, no eating or sleeping 
  34. ' etc.), the termites can do 12 different things.  They can move a random  
  35. ' distance in one of 8 different directions (in practise this means they can 
  36. ' move anywhere, as long as SPEED>1).  They have a 1/12 chance of moving in
  37. ' any given direction.  They have a 1/12 chance of carrying on in the same 
  38. ' direction they were digging in last 'turn'.  They also have a 2/12 chance of 
  39. ' carrying on in almost the same direction they were last time, (a slight
  40. ' deviation), and a 1/12 chance of resting.
  41. '
  42. ' But even though it is all random, they seem to build a 'nest', and to send 
  43. ' out lone termites to dig narrow passages of exploration.  It's all good
  44. ' fun, really. 
  45. '  
  46. '
  47. ' Hold down the left mouse button to quit.  Change the two variables below to
  48. ' alter the number of termites on each side and the maximum digging speed. 
  49. '
  50. NUMBER=25 : SPEED=8
  51. '
  52. '
  53. '
  54. Hide On : Rem Get rid of the mouse pointer 
  55. '
  56. Dim X1(NUMBER),Y1(NUMBER),X2(NUMBER),Y2(NUMBER),SX1(NUMBER),SY1(NUMBER)
  57. Dim SX2(NUMBER),SY2(NUMBER),OZ1(NUMBER),OZ2(NUMBER)
  58. ' Sort out arrays for X and Y positions of every termite , and the last  
  59. ' direction they were diggin in
  60. '
  61. Randomize Timer : Rem Make sure the random numbers are always different
  62. '
  63. BLUEX=Rnd(318) : BLUEY=Rnd(198)
  64. REDX=Rnd(318) : REDY=Rnd(198)
  65. If BLUEX=<0 Then BLUEX=1 : If BLUEY<=0 Then BLUEY=0
  66. If REDX=<0 Then REDX=1 : If REDY<=0 Then REDY=0
  67. '
  68. ' Sort out random start positions for each base
  69. '
  70. For T=1 To NUMBER
  71.    SX1(T)=REDX : SX2(T)=BLUEX
  72.    SY1(T)=REDY : SY2(T)=BLUEY
  73.    X1(T)=SX1(T) : Y1(T)=SY1(T)
  74.    X2(T)=SX2(T) : Y2(T)=SY2(T)
  75. Next 
  76. ' And put every termite in the base
  77. '
  78. Screen Open 0,320,200,4,Lowres
  79. Flash Off : Curs Off 
  80. Palette $765,$F00,$F,$FFF : Cls 0
  81. '
  82. ' Sort out screen and palette
  83. '
  84. Repeat 
  85.    For T=1 To NUMBER
  86.       X=Rnd(SPEED) : Y=Rnd(SPEED)
  87.       ' Random X and Y digging speeds
  88.       Z=Rnd(11)
  89.       ' Random digging direction for this termite :
  90.       ' 1 is up, 3 is right, etc.  
  91.       ' 0, 9 or 10 means carry on digging in the same or in a similar
  92.       ' direction, giving them them a sort of goal.  If 11 comes up then 
  93.       ' they have a rest 
  94.       If Z=0
  95.          Z=OZ1(T)
  96.       End If 
  97.       If Z=9
  98.          Z=OZ1(T)+1
  99.          If Z>7
  100.             Z=1
  101.          End If 
  102.          ' make sure they dig 
  103.       End If 
  104.       If Z=10
  105.          Z=OZ1(T)-1
  106.          If Z<0
  107.             Z=7
  108.          End If 
  109.       End If 
  110.       If Z=1
  111.          Add Y1(T),-Y
  112.       End If 
  113.       If Z=2
  114.          Add X1(T),X : Add Y1(T),-Y
  115.       End If 
  116.       If Z=3
  117.          Add X1(T),X
  118.       End If 
  119.       If Z=4
  120.          Add X1(T),X : Add Y1(T),Y
  121.       End If 
  122.       If Z=5
  123.          Add Y1(T),Y
  124.       End If 
  125.       If Z=6
  126.          Add Y1(T),Y : Add X1(T),-X
  127.       End If 
  128.       If Z=7
  129.          Add X1(T),-X
  130.       End If 
  131.       If Z=8
  132.          Add X1(T),-X : Add Y1(T),-Y
  133.       End If 
  134.       OZ1(T)=Z : Rem store the last digging direction 
  135.       '
  136.       If X1(T)<0 Then X1(T)=0 : If X1(T)>319 Then X1(T)=319
  137.       If Y1(T)<0 Then Y1(T)=0 : If Y1(T)>199 Then Y1(T)=199
  138.       Ink 1 : Draw SX1(T),SY1(T) To X1(T),Y1(T)
  139.       SX1(T)=X1(T) : SY1(T)=Y1(T)
  140.       ' Make sure the termites are within the screen boundaries then 
  141.       ' store the old values, after drawing a line where it has been 
  142.       ' digging
  143.       '
  144.       ' And now the blue termites... 
  145.       '
  146.       X=Rnd(SPEED) : Y=Rnd(SPEED)
  147.       Z=Rnd(11)
  148.       If Z=0
  149.          Z=OZ2(T)
  150.       End If 
  151.       If Z=9
  152.          Z=OZ2(T)+1
  153.          If Z>7
  154.             Z=1
  155.          End If 
  156.       End If 
  157.       If Z=10
  158.          Z=OZ2(T)-1
  159.          If Z<1
  160.             Z=7
  161.          End If 
  162.       End If 
  163.       If Z=1
  164.          Add Y2(T),-Y
  165.       End If 
  166.       If Z=2
  167.          Add X2(T),X : Add Y2(T),-Y
  168.       End If 
  169.       If Z=3
  170.          Add X2(T),X
  171.       End If 
  172.       If Z=4
  173.          Add X2(T),X : Add Y2(T),Y
  174.       End If 
  175.       If Z=5
  176.          Add Y2(T),Y
  177.       End If 
  178.       If Z=6
  179.          Add Y2(T),Y : Add X2(T),-X
  180.       End If 
  181.       If Z=7
  182.          Add X2(T),-X
  183.       End If 
  184.       If Z=8
  185.          Add X2(T),-X : Add Y2(T),-Y
  186.       End If 
  187.       OZ2(T)=Z
  188.       '
  189.       If X2(T)<0 Then X2(T)=0 : If X2(T)>319 Then X2(T)=319
  190.       If Y2(T)<0 Then Y2(T)=0 : If Y2(T)>199 Then Y2(T)=199
  191.       Ink 2 : Draw SX2(T),SY2(T) To X2(T),Y2(T)
  192.       SX2(T)=X2(T) : SY2(T)=Y2(T)
  193.    Next 
  194. Until Mouse Key=1
  195. '
  196. Home : Cdown : Cdown : Cdown : Cdown : Cdown 
  197. Pen 3 : Centre "Termites v1.00"
  198. Cdown : Cdown : Cdown : Cdown 
  199. Centre "Another great program from..."
  200. Cdown : Cdown : Cdown : Cdown 
  201. Centre "RipperSoft"
  202. Cdown : Cdown : Cdown : Cdown 
  203. Centre "Press mouse to return to editor"
  204. Wait 25
  205. Repeat 
  206. Until Mouse Key<>0
  207. Fade 2
  208. Edit